﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AccountFormsCtrlLib;
using DevComponents.DotNetBar;
using DevelopStudio.Basic;
using System.IO;
using CommonManage = DevelopStudio.Common.Manage;
using CommonBasic = DevelopStudio.Common.Basic;
using DevelopStudio.Control;
using DevelopStudio.Control.FormPage;
using DevComponents.DotNetBar.Controls;

namespace DevelopStudio
{
    public partial class CodeReviewPlatform : ControlBase
    {
        private ChangelistInfo changelistInfo;

        public ChangelistInfo ChangelistInfo
        {
            get
            {
                return this.changelistInfo;
            }

            set
            {
                if (value == null)
                {
                    MessageBox.Show(Properties.Resources.ErrorNullChangelistInPlatform);
                    return;
                }

                if (this.changelistInfo != value)
                {
                    if (this.changelistInfo != null)
                    {
                        this.changelistInfo.StatusChanged -= changelistInfo_StatusChanged;
                        this.changelistInfo.ErrorStatusChanged -= changelistInfo_ErrorCodeChanged;
                    }

                    this.changelistInfo = value;
                    this.changelistInfo.StatusChanged += changelistInfo_StatusChanged;
                    this.changelistInfo.ErrorStatusChanged += changelistInfo_ErrorCodeChanged;
                    this.labelChangelistTitle.Text = this.changelistInfo.Name;
                    this.labelPlatform.Text = this.changelistInfo.WorkingCopyPath;
                    this.labelXFrom.Text = string.Format(Properties.Resources.FromTitle, this.changelistInfo.SendedBy.Name);
                    this.StatusChanged(this.changelistInfo);
                    this.ErrorCodeChanged(this.changelistInfo);
                    this.DisplayBugs();
                }
            }
        }

        void changelistInfo_ErrorCodeChanged(object sender, CommonBasic.StatusEventArgs e)
        {
            this.Invoke(new Action(() =>
            {
                ChangelistInfo owner = sender as ChangelistInfo;

                if (owner != this.changelistInfo)
                {
                    throw new InvalidDataException(Properties.Resources.ExceptionHandler);
                }

                this.ErrorCodeChanged(owner);
            }));
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.pictureBoxError.Image = new Bitmap(Properties.Resources.critical.ToBitmap(), new Size(16, 16));

            if (this.ChangelistInfo.BugInfos.Count <= 0)
            {
                this.ChangelistInfo.ErrorInfo = new ChanglistErrorInfo(AppEnum.ChangelistErrorCode.BugAssigned, Properties.Resources.ErrorChangelistWithNoBug);
            }
        }

        private void ErrorCodeChanged(ChangelistInfo changelist)
        {
            switch (changelist.ErrorInfo.ErrorCode)
            {
                case AppEnum.ChangelistErrorCode.BugAssigned:
                    {
                        this.buttonCodeReview.Enabled = false;
                    }
                    break;
                case AppEnum.ChangelistErrorCode.CodeReview:
                    {
                        this.buttonCodeReview.Enabled = false;
                    }
                    break;
                default:
                    break;
            }

            if (changelist.ErrorInfo.ErrorCode == AppEnum.ChangelistErrorCode.None)
            {
                this.panelError.Visible = false;
                this.labelErrorText.Text = string.Empty;
            }
            else
            {
                this.panelError.Visible = true;
                this.labelErrorText.Text = changelistInfo.ErrorInfo.Message;
            }

            this.toolTip.SetToolTip(this.labelErrorText, this.labelErrorText.Text);
            //this.toolTip.SetToolTip(this.pictureBoxError, this.labelErrorText.Text);
            //this.toolTip.SetToolTip(this.panelError, this.labelErrorText.Text);
        }

        private void StatusChanged(ChangelistInfo changelist)
        {
            switch (changelist.Status)
            {
                case (int)AppEnum.ChangelistStatus.ReceiveReviewing:
                    {
                        this.buttonCodeReview.Enabled = true;
                    }
                    break;
                case (int)AppEnum.ChangelistStatus.Approved:
                case (int)AppEnum.ChangelistStatus.ApprovedWithComment:
                case (int)AppEnum.ChangelistStatus.Rejected:
                    {
                        OnDisplayChanged(this, new CommonBasic.StatusEventArgs(false, true));
                    }
                    break;
                default:
                    {
                        MessageBox.Show(Properties.Resources.ErrorAddCodeReviewChangelistToCodeReviewPlatform);
                        return;
                    }
            }

            if (changelist.Status == (int)AppEnum.ChangelistStatus.SendReviewing)
            {
                this.buttonCodeReview.Text = Properties.Resources.CancelReview;
            }
            else
            {
                this.buttonCodeReview.Text = Properties.Resources.CodeReview;
            }
        }

        protected void changelistInfo_StatusChanged(object sender, CommonBasic.StatusEventArgs e)
        {
            this.Invoke(new Action(() => {
                ChangelistInfo owner = sender as ChangelistInfo;

                if (owner != this.changelistInfo)
                {
                    throw new InvalidDataException(Properties.Resources.ExceptionHandler);
                }

                this.StatusChanged(owner);
            }));
        }

        public CodeReviewPlatform()
        {
            InitializeComponent();
        }

        private void DisplayBugs()
        {
            this.buttonCodeReview.Text = string.Format(Properties.Resources.Review, this.changelistInfo.BugInfos.Count);
        }

        private void buttonViewChange_Click(object sender, EventArgs e)
        {
            try
            {
                string execFileFullPath = Path.Combine(Application.StartupPath, AppConst.DiffToolPath);
                string fileFullName = Path.GetTempFileName() + CommonBasic.AppConst.DiffToolFileSuffix;

                if (Utility.PackageDiffInfo(this.changelistInfo, fileFullName) == false)
                {
                    MessageBox.Show(string.Format(Properties.Resources.ErrorPackageToFile, fileFullName));
                    return;
                }

                Utility.ExecuteCmd(execFileFullPath, fileFullName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(Utility.TraceException(ex), this.ParentForm.Text);
                return;
            }
        }

        private void buttonCodeReview_Click(object sender, EventArgs e)
        {
            try
            {
                AppEnum.ChangelistStatus nextStatus;

                using (CodeReviewForm dlg = new CodeReviewForm(this.ChangelistInfo))
                {
                    if (dlg.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }

                    nextStatus = dlg.NextChangelistStatus;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Utility.TraceException(ex), this.ParentForm.Text);
                return;
            }
        }
    }
}
